package com.manage.service.impl;

import com.manage.mapper.primary.OrderMapperPrimary;
import com.manage.mapper.secondary.OrderMapperSecondary;
import com.manage.model.dto.OrderDTO;
import com.manage.service.MoreDataSourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 多数据源操作
 * @author duaiyu
 * @version V1.0
 * @date 2022/5/29
 * 来源：自研
 */
@Service
public class MoreDataSourceServiceImpl implements MoreDataSourceService {
    @Resource
    OrderMapperPrimary orderMapperPrimary;
    @Resource
    OrderMapperSecondary orderMapperSecondary;

    @Override
    public List<OrderDTO> queryPrimaryOrder() {
        List<OrderDTO> list = orderMapperPrimary.queryPrimaryOrder();
        return list;
    }

    @Override
    public List<OrderDTO> querySecondaryOrder() {
        List<OrderDTO> list = orderMapperSecondary.querySecondaryOrder();
        return list;
    }

    @Override
    public Integer insertPrimary(OrderDTO order) {
        int result = orderMapperPrimary.insertPrimary(order);
        return result;
    }

    @Override
    public Integer insertSecondary(OrderDTO order) {
        int result = orderMapperSecondary.insertSecondary(order);
        return result;
    }

    @Override
    public Integer dataCopy() {
        int result = 0;
        List<OrderDTO> primaryData = orderMapperPrimary.queryPrimaryOrder();
        for(int i = 0; i<primaryData.size(); i++){
            result = orderMapperSecondary.insertSecondary(primaryData.get(i));
            if(result == 0){
                result = i;
            }
        }
        return result;
    }
}
